using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Windows.Interop;
using System.Windows.Media.Media3D;
using System.Windows.Media.Animation;
using System.IO;
using System.Runtime.InteropServices;
using System.Data;
using System.Windows.Threading;
using System.Windows.Forms;

namespace FaceCat
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 创建窗体
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            InputMethod.SetIsInputMethodEnabled(this, true);
            InputMethod.SetIsInputMethodSuspended(this, true);
            this.SourceInitialized += new EventHandler(MainWindow_SourceInitialized);
            m_uiView = new FCUIView();
            Grid1.Children.Add(m_uiView);
            m_uiView.onLoad();
        }

        private MainFrame m_mainFrame;

        /// <summary>
        /// 绘图对象
        /// </summary>
        private FCUIView m_uiView;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void MainWindow_SourceInitialized(object sender, EventArgs e)
        {
            IntPtr hwnd = ((HwndSource)PresentationSource.FromVisual(m_uiView)).Handle;
            HwndSource.FromHwnd(hwnd).AddHook(new HwndSourceHook(WndProc));
            m_uiView.m_host.setHWnd(hwnd);
            m_mainFrame = new MainFrame();
            m_mainFrame.setNative(m_uiView.m_native);
            string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
            <html xmlns=""facecat"">
            <body>
                <div bordercolor=""none"" name=""divInner"" dock=""fill"">
                    <div type=""tab"" dock=""fill"" selectedindex=""0"" backcolor=""none"" bordercolor=""none""
                    name=""tabFunc"">
                        <div type=""tabpage"" text=""主界面"" name=""divMain"" backcolor=""none"" bordercolor=""none"">
                        <div type=""splitlayout"" layoutstyle=""lefttoright"" bordercolor=""none"" dock=""fill""
                                size=""610,610"" candragsplitter=""true"" splitterposition=""610,1"" name=""divCodingRight2"">
                                <div type=""custom"" cid=""securitylist"" name=""gridStocks"" headerheight=""30"" dock=""fill""
                                    gridlinecolor=""none"" bordercolor=""none"" showvscrollbar=""true"" showhscrollbar=""true""
                                    allowpreviewsevent=""true"" allowdragscroll=""true"">
                                    <tr>
                                        <th name=""colP0"" text=""序"" width=""40"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" />
                                        <th name=""colP1"" text=""代码"" width=""70"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" />
                                        <th name=""colP2"" text=""名称"" width=""70"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" />
                                        <th name=""colP3"" text=""现价"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP4"" text=""涨幅"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP5"" text=""涨跌"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP9"" text=""总量"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP10"" text=""总额"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP11"" text=""量比"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP12"" text=""PE动"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP13"" text=""振幅"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP14"" text=""换手率"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP15"" text=""总市值"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP16"" text=""流值"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP17"" text=""行业分类板块"" width=""80"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""center""/>
                                        <th name=""colP18"" text=""涨停价"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP19"" text=""跌停价"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP20"" text=""金比"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP21"" text=""涨跌比"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP22"" text=""涨速"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP23"" text=""净资产收益率"" width=""100"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP24"" text=""自设指标"" width=""80"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    </tr>
                                </div>
                                <div type=""splitlayout"" layoutstyle=""toptobottom"" bordercolor=""none"" dock=""fill""
                                    size=""400,400"" candragsplitter=""true"" splitterposition=""400,1"">
                                    <div type=""splitlayout"" layoutstyle=""righttoleft"" bordercolor=""none"" dock=""fill""
                                        size=""400,400"" candragsplitter=""true"" splitterposition=""200,1"">
                                        <div type=""custom"" cid=""latestdiv"" name=""divLatest"" />
                                        <div type=""custom"" cid=""mychart"" name=""mainChart1"" cycle=""0"" />
                                    </div>
                                    <div type=""splitlayout"" layoutstyle=""lefttoright"" bordercolor=""none"" dock=""fill""
                                        size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                        <div type=""custom"" cid=""mychart"" name=""mainChart2"" cycle=""5"" />
                                        <div type=""custom"" cid=""mychart"" name=""mainChart3"" cycle=""1440"" />
                                    </div>
                                </div>
                            </div>
                            </div>
                            <div type=""tabpage"" text=""多K线"" name=""divMulti"" backcolor=""none"" bordercolor=""none"">
                                <div type=""splitlayout"" layoutstyle=""lefttoright"" backcolor=""none"" bordercolor=""none""
                                    dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""133,1"">
                                    <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                        dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""267,1"">
                                        <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                            dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                            <div type=""custom"" cid=""mychart"" name=""chart1"" cycle=""1"" nativerefresh=""true"" candledivpercent=""1""
                                                voldivpercent=""0"" />
                                            <div type=""custom"" cid=""mychart"" name=""chart2"" cycle=""5"" nativerefresh=""true"" candledivpercent=""1""
                                                voldivpercent=""0"" />
                                        </div>
                                        <div type=""custom"" cid=""mychart"" name=""chart3"" cycle=""10"" nativerefresh=""true"" candledivpercent=""1""
                                            voldivpercent=""0"" />
                                    </div>
                                    <div type=""splitlayout"" layoutstyle=""lefttoright"" backcolor=""none"" bordercolor=""none""
                                        dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                        <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                            dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""267,1"">
                                            <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                                dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                                <div type=""custom"" cid=""mychart"" name=""chart4"" cycle=""15"" nativerefresh=""true"" candledivpercent=""1""
                                                    voldivpercent=""0"" />
                                                <div type=""custom"" cid=""mychart"" name=""chart5"" cycle=""20"" nativerefresh=""true"" candledivpercent=""1""
                                                    voldivpercent=""0"" />
                                            </div>
                                            <div type=""custom"" cid=""mychart"" name=""chart6"" cycle=""30"" nativerefresh=""true"" candledivpercent=""1""
                                                voldivpercent=""0"" />
                                        </div>
                                        <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                            dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""267,1"">
                                            <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                                dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                                <div type=""custom"" cid=""mychart"" name=""chart7"" cycle=""1440"" nativerefresh=""true""
                                                    candledivpercent=""1"" voldivpercent=""0"" />
                                                <div type=""custom"" cid=""mychart"" name=""chart8"" cycle=""10080"" nativerefresh=""true""
                                                    candledivpercent=""1"" voldivpercent=""0"" />
                                            </div>
                                            <div type=""custom"" cid=""mychart"" name=""chart9"" cycle=""43200"" nativerefresh=""true""
                                                candledivpercent=""1"" voldivpercent=""0"" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div type=""tabpage"" text=""输入框"" name=""divTextBox"" backcolor=""none"" bordercolor=""none"">
                            <input type=""text"" name=""TextBox"" location=""25,25"" size=""100,20""  />
                            <input type=""text"" name=""TextBox2"" location=""25,65"" size=""100,20""  />
                            <input type=""text"" name=""TextBox3"" location=""25,106"" size=""100,20""  />
                        </div>
                        </div>
                    </div>
            </body>
            </html>
            ";
            m_mainFrame.loadXml(xml, null);
        }

        /// <summary>
        /// Windows消息
        /// </summary>
        public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (m_uiView.m_host.onMessage(hwnd, msg, wParam, lParam) > 0)
            {
                handled = true;
            }
            return IntPtr.Zero;
        }

        /// <summary>
        /// 窗体关闭方法
        /// </summary>
        /// <param name="e">参数</param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            Process.GetCurrentProcess().Kill();
        }

        /// <summary>
        /// 大小改变方法
        /// </summary>
        /// <param name="sizeInfo">参数</param>
        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            base.OnRenderSizeChanged(sizeInfo);
            m_uiView.Height = sizeInfo.NewSize.Height - 30;
        }
    }
}
